// import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
// import 'dart:ui' as ui;
// import 'package:path_provider/path_provider.dart';
// import 'helper/debouncer.dart';
import 'painter/painter.dart';
import 'text/config.dart';

class TextBoxConfigController {
  final CanvasTextConfig? initialConfig;
  CanvasTextConfig? _currentConfig;
  final EdgeInsets padding;
  final String label;
  // static String subPath = '/Documents/VIIV/captions';
  double? Function()? scaleSetter;

  CanvasTextConfig? get config => _currentConfig;
  Directory? appPath;
  Size? _size;
  Size get size => _size ?? Size.zero;
  Function(Size size)? onSizeUpdate;

  // Debouncer debouncer = Debouncer(delay: const Duration(milliseconds: 100));

  TextBoxConfigController(this.label,
      {required this.padding,
      this.initialConfig,
      this.onSizeUpdate,
      this.scaleSetter}) {
    _currentConfig = initialConfig;
  }

  void updateConfig(CanvasTextConfig config) {
    _currentConfig = config;
  }

  TextBoxPainter? get painter => config != null
      ? TextBoxPainter(config: config!, boxPadding: padding)
      : null;

  // Future<String> renderImageAndSave() async {
  //   final ui.PictureRecorder recorder = ui.PictureRecorder();
  //   appPath ??= await getApplicationDocumentsDirectory();
  //   Rect newSize;
  //   Size? mSize;
  //   Canvas canvas = Canvas(recorder);
  //   double ratio = mSize != null ? 1080 / mSize.width : 1;
  //   Size imageSize;
  //   print('mSize - $mSize');
  //   if (mSize != null) {
  //     imageSize = Size(mSize.width * ratio, mSize.height * ratio);
  //     canvas.save();
  //     canvas.scale(ratio);
  //     newSize = painter!.paint(canvas, Size.infinite);
  //   } else {
  //     newSize = painter!.paint(canvas, Size.infinite);

  //     imageSize = newSize.size;
  //     canvas.save();
  //     canvas.scale(ratio);
  //   }

  //   print('newSize - $newSize');
  //   canvas.restore();

  //   var outerSize = Size(
  //       newSize.size.width,
  //       newSize.size.height +
  //           ((padding.top + padding.bottom) / (scaleSetter?.call() ?? 1.0)));
  //   updateSize(outerSize);

  //   ui.Image renderedImage = await recorder
  //       .endRecording()
  //       .toImage(imageSize.width.floor(), imageSize.height.floor());
  //   var pngBytes =
  //       await renderedImage.toByteData(format: ui.ImageByteFormat.png);

  //   File saveFile = File(
  //       '${appPath!.path}$subPath/${DateTime.now().millisecondsSinceEpoch}.png');

  //   if (!(await saveFile.exists())) {
  //     await saveFile.create(recursive: true);
  //   }
  //   await saveFile.writeAsBytes(pngBytes!.buffer.asUint8List(), flush: true);

  //   return saveFile.path;
  // }

  void updateSize(Size size) {
    _size = size;
    onSizeUpdate?.call(size);
  }
}
